#define _CRT_SECURE_NO_WARNINGS 1
#include"Sort.h"
#include"stack.h"
void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}

	printf("\n");
}

void Swap(int* m, int* n)
{
	int mid = *m;
	*m = *n;
	*n = mid;
}

void InsertSort(int* a, int n)
{
	for (int i = 1; i < n; i++)
	{
		for (int j = i; j > 0; j--)
		{
			if (a[j] < a[j - 1])
				Swap(&a[j], &a[j - 1]);
			else
				break;
		}
	}
}

void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			for (int j = i + gap; j >= gap; j -= gap)
			{
				if (a[j] < a[j - gap])
					Swap(&a[j], &a[j - gap]);
				else
					break;
			}
		}
	}
}

void SelectSort(int* a, int n)
{
	int min = 0;
	for (int i = 0; i < n - 1; i++)
	{
		min = i;
		for (int j = i+1; j < n; j++)
		{
			if (a[j] < a[min])
				min = j;
		}

		Swap(&a[i], &a[min]);
	}
}

void TwoWaySelectSort(int* a, int n)
{
	int min = 0;
	int max = 0;
	for (int i = 0; i <= n / 2; i++)
	{
		min = i;
		max = i;
		for (int j = i + 1; j < n - i; j++)
		{
			if (a[j] < a[min])
				min = j;
			if (a[j] > a[max])
				max = j;
		}

		Swap(&a[i], &a[min]);
		if (max == i)
			max = min;
		Swap(&a[n-i-1], &a[max]);
	}
}

void BubbleSort(int* a, int n)
{
	for (int i = 1; i < n; i++)
	{
		for (int j = 0; j < n - i; j++)
		{
			if (a[j] > a[j + 1])
				Swap(&a[j + 1], &a[j]);
		}
	}
}

void OptimizationBubbleSort(int* a, int n)
{
	for (int i = 1; i < n; i++)
	{
		int exchange = 0;
		for (int j = 0; j < n - i; j++)
		{
			if (a[j] > a[j + 1])
			{
				Swap(&a[j + 1], &a[j]);
				exchange = 1;
			}
		}

		if (exchange == 0)
			break;
	}
}

int GetMidIndex(int* a, int left, int right)
{
	int mid = left + (right - left) >> 1;
	if (a[left] > a[mid])
	{
		if (a[right] < a[mid])
			return mid;
		else if (a[left] > a[right])
			return right;
		else
			return left;
	}
	else
	{
		if (a[right] < a[left])
			return left;
		else if (a[mid] > a[right])
			return right;
		else
			return mid;
	}
}

int Partion1(int* a, int left, int right)
{
	int mini = GetMidIndex(a, left, right);
	Swap(&a[mini], &a[left]);
	int keyi = left;
	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
			--right;

		while (left < right && a[left] <= a[keyi])
			++left;

		Swap(&a[left], &a[right]);
	}
	Swap(&a[left], &a[keyi]);
	return left;
}

int Partion2 (int* a, int left, int right)
{
	int mini = GetMidIndex(a, left, right);
	Swap(&a[mini], &a[left]);
	int pivot = left;
	while (left < right)
	{
		while (left < right && a[right] >= a[pivot])
			right--;

		Swap(&a[right], &a[pivot]);
		pivot = right;

		while (left < right&& a[left] <= a[pivot])
				left++;

		Swap(&a[left], &a[pivot]);
		pivot = left;
	}
	return pivot;
}

int Partion3(int* a, int left, int right)
{
	int keyi = left;
	int prev = left;
	int cur = left;
	while (cur < right)
	{
		cur++;
		if (a[cur] < a[keyi])
		{
			prev++;
			if(cur!=prev)
				Swap(&a[cur], &a[prev]);
		}
	}

	Swap(&a[keyi], &a[prev]);
	return prev;
}

void QuickSort(int* a, int left, int right)
{
	if (left >= right)
		return;
	int keyi = Partion3(a, left, right);
	QuickSort(a, left, keyi - 1);
	QuickSort(a, keyi + 1, right);
}

void QuickSortNonR(int* a, int left, int right)
{
	ST stack;
	StackInit(&stack);
	StackPush(&stack, left);
	StackPush(&stack, right);
	while (stack.top)
	{
		int end = StackTop(&stack);
		StackPop(&stack);
		int begin = StackTop(&stack);
		StackPop(&stack);
		int keyi = Partion3(a, begin, end);
		if (begin < keyi - 1)
		{
			StackPush(&stack, begin);
			StackPush(&stack, keyi - 1);
		}

		if (keyi + 1 < end)
		{
			StackPush(&stack, keyi + 1);
			StackPush(&stack, end);
		}
	}
	StackDestroy(&stack);
}

void _MergeSort(int* a, int left, int right, int* tmp)
{
	if (left >= right)
		return;
	int mid = (left + right) / 2;
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	_MergeSort(a, begin1, end1, tmp);
	_MergeSort(a, begin2, end2, tmp);
	int i = left;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}

	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}

	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}

	for (int j = left; j <= right; ++j)
	{
		a[j] = tmp[j];
	}
}

void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}

	_MergeSort(a, 0, n - 1, tmp);

	free(tmp);
	tmp = NULL;
}

void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int)*n);
	if (tmp == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i+gap, end2 = i + 2*gap-1;
			if (end1 >= n)
			{
				end1 = n - 1;
			}

			if (begin2 >= n)
			{
				begin2 = n;
				end2 = n - 1;
			}
	
			if (end2 >= n)
			{
				end2 = n - 1;
			}

			int index = i;
			while (begin1 <= end1 && begin2 <= end2)
			{
	
				if (a[begin1] < a[begin2])
				{
					tmp[index++] = a[begin1++];
				}
				else
				{
					tmp[index++] = a[begin2++];
				}
			}
	
			while (begin1 <= end1)
			{
	
				tmp[index++] = a[begin1++];
			}
	
			while (begin2 <= end2)
			{
	
				tmp[index++] = a[begin2++];
			}
		}
			
		for (int i = 0; i < n; ++i)
		{
			a[i] = tmp[i];
		}
	
		gap *= 2;
	}
	
	free(tmp);
	tmp = NULL;
}

void CountSort(int* a, int n)
{
	int max = a[0], min = a[0];
	for (int i = 1; i < n; ++i)
	{
		if (a[i] > max)
		{
			max = a[i];
		}

		if (a[i] < min)
		{
			min = a[i];
		}
	}

	int range = max - min + 1;
	int* count = (int*)malloc(sizeof(int) * range);
	memset(count, 0, sizeof(int) * range);
	if (count == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}

	for (int i = 0; i < n; ++i)
	{
		count[a[i] - min]++;
	}

	int j = 0;
	for (int i = 0; i < range; ++i)
	{
		while (count[i]--)
		{
			a[j++] = i + min;
		}
	}
}